package com.openbravo.pos.customers;

import com.openbravo.basic.BasicException;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.editor.JEditorKeys;
import com.openbravo.editor.JEditorString;
import com.openbravo.format.Formats;
import com.openbravo.pos.forms.AppLocal;
import com.openbravo.pos.forms.AppUser;
import com.openbravo.pos.forms.AppUserView;
import com.openbravo.pos.forms.AppView;
import com.openbravo.pos.forms.BeanFactoryApp;
import com.openbravo.pos.forms.BeanFactoryException;
import com.openbravo.pos.forms.DataLogicSales;
import com.openbravo.pos.forms.DataLogicSystem;
import com.openbravo.pos.forms.JPanelView;
import com.openbravo.pos.payment.JPaymentSelect;
import com.openbravo.pos.payment.JPaymentSelectCustomer;
import com.openbravo.pos.payment.PaymentInfo;
import com.openbravo.pos.payment.PaymentInfoTicket;
import com.openbravo.pos.printer.TicketParser;
import com.openbravo.pos.printer.TicketPrinterException;
import com.openbravo.pos.scripting.ScriptEngine;
import com.openbravo.pos.scripting.ScriptException;
import com.openbravo.pos.scripting.ScriptFactory;
import com.openbravo.pos.ticket.TicketInfo;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.LayoutStyle.ComponentPlacement;

public class CustomersPayment extends JPanel
  implements JPanelView, BeanFactoryApp
{
  private AppView app;
  private DataLogicCustomers dlcustomers;
  private DataLogicSales dlsales;
  private DataLogicSystem dlsystem;
  private TicketParser ttp;
  private JPaymentSelect paymentdialog;
  private CustomerInfoExt customerext;
  private DirtyManager dirty;
  private JButton btnCustomer;
  private JButton btnPay;
  private JButton btnSave;
  private JEditorString editorcard;
  private JButton jButton1;
  private JLabel jLabel1;
  private JLabel jLabel12;
  private JLabel jLabel2;
  private JLabel jLabel3;
  private JLabel jLabel5;
  private JLabel jLabel6;
  private JLabel jLabel7;
  private JPanel jPanel1;
  private JPanel jPanel2;
  private JPanel jPanel3;
  private JPanel jPanel4;
  private JPanel jPanel5;
  private JPanel jPanel6;
  private JSeparator jSeparator1;
  private JEditorKeys m_jKeys;
  private JTextField txtCard;
  private JTextField txtCurdate;
  private JTextField txtCurdebt;
  private JTextField txtMaxdebt;
  private JTextField txtName;
  private JEditorString txtNotes;
  private JTextField txtTaxId;
private MessageInf localMessageInf;

  public CustomersPayment()
  {
    initComponents();
    this.editorcard.addEditorKeys(this.m_jKeys);
    this.txtNotes.addEditorKeys(this.m_jKeys);
    this.dirty = new DirtyManager();
    this.txtNotes.addPropertyChangeListener("Text", this.dirty);
  }

  public void init(AppView paramAppView)
    throws BeanFactoryException
  {
    this.app = paramAppView;
    this.dlcustomers = ((DataLogicCustomers)paramAppView.getBean("com.openbravo.pos.customers.DataLogicCustomers"));
    this.dlsales = ((DataLogicSales)paramAppView.getBean("com.openbravo.pos.forms.DataLogicSales"));
    this.dlsystem = ((DataLogicSystem)paramAppView.getBean("com.openbravo.pos.forms.DataLogicSystem"));
    this.ttp = new TicketParser(paramAppView.getDeviceTicket(), this.dlsystem);
  }

  public Object getBean()
  {
    return this;
  }

  public String getTitle()
  {
    return AppLocal.getIntString("Menu.CustomersPayment");
  }

  public void activate()
    throws BasicException
  {
    this.paymentdialog = JPaymentSelectCustomer.getDialog(this);
    this.paymentdialog.init(this.app);
    resetCustomer();
    this.editorcard.reset();
    this.editorcard.activate();
  }

  public boolean deactivate()
  {
    if (this.dirty.isDirty())
    {
      int i = JOptionPane.showConfirmDialog(this, AppLocal.getIntString("message.wannasave"), AppLocal.getIntString("title.editor"), 1, 3);
      if (i == 0)
      {
        save();
        return true;
      }
      return i == 1;
    }
    return true;
  }

  public JComponent getComponent()
  {
    return this;
  }

  private void editCustomer(CustomerInfoExt paramCustomerInfoExt)
  {
    this.customerext = paramCustomerInfoExt;
    this.txtTaxId.setText(paramCustomerInfoExt.getTaxid());
    this.txtName.setText(paramCustomerInfoExt.getName());
    this.txtCard.setText(paramCustomerInfoExt.getCard());
    this.txtNotes.reset();
    this.txtNotes.setText(paramCustomerInfoExt.getNotes());
    this.txtMaxdebt.setText(Formats.CURRENCY.formatValue(paramCustomerInfoExt.getMaxdebt()));
    this.txtCurdebt.setText(Formats.CURRENCY.formatValue(paramCustomerInfoExt.getCurdebt()));
    this.txtCurdate.setText(Formats.DATE.formatValue(paramCustomerInfoExt.getCurdate()));
    this.txtNotes.setEnabled(true);
    this.dirty.setDirty(false);
    this.btnSave.setEnabled(true);
    this.btnPay.setEnabled((paramCustomerInfoExt.getCurdebt() != null) && (paramCustomerInfoExt.getCurdebt().doubleValue() > 0.0D));
  }

  private void resetCustomer()
  {
    this.customerext = null;
    this.txtTaxId.setText(null);
    this.txtName.setText(null);
    this.txtCard.setText(null);
    this.txtNotes.reset();
    this.txtMaxdebt.setText(null);
    this.txtCurdebt.setText(null);
    this.txtCurdate.setText(null);
    this.txtNotes.setEnabled(false);
    this.dirty.setDirty(false);
    this.btnSave.setEnabled(false);
    this.btnPay.setEnabled(false);
  }

  private void readCustomer()
  {
    try
    {
      CustomerInfoExt localCustomerInfoExt = this.dlsales.findCustomerExt(this.editorcard.getText());
      if (localCustomerInfoExt == null)
      {
        MessageInf localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotfindcustomer"));
        localMessageInf.show(this);
      }
      else
      {
        editCustomer(localCustomerInfoExt);
      }
    }
    catch (BasicException localBasicException)
    {
      MessageInf localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotfindcustomer"), localBasicException);
      localMessageInf.show(this);
    }
    this.editorcard.reset();
    this.editorcard.activate();
  }

  private void save()
  {
    this.customerext.setNotes(this.txtNotes.getText());
    try
    {
      this.dlcustomers.updateCustomerExt(this.customerext);
      editCustomer(this.customerext);
    }
    catch (BasicException localBasicException)
    {
      MessageInf localMessageInf = new MessageInf(-67108864, AppLocal.getIntString("message.nosave"), localBasicException);
      localMessageInf.show(this);
    }
  }

  private void printTicket(String paramString, TicketInfo paramTicketInfo, CustomerInfoExt paramCustomerInfoExt)
  {
    String str = this.dlsystem.getResourceAsXML(paramString);
    Object localObject;
    if (str == null)
    {
      localObject = new MessageInf(-33554432, AppLocal.getIntString("message.cannotprintticket"));
      ((MessageInf)localObject).show(this);
    }
    else
    {
      try
      {
        localObject = ScriptFactory.getScriptEngine("velocity");
        ((ScriptEngine)localObject).put("ticket", paramTicketInfo);
        ((ScriptEngine)localObject).put("customer", paramCustomerInfoExt);
        this.ttp.printTicket(((ScriptEngine)localObject).eval(str).toString());
      }
      catch (ScriptException localScriptException)
      {
        localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotprintticket"), localScriptException);
        localMessageInf.show(this);
      }
      catch (TicketPrinterException localTicketPrinterException)
      {
        MessageInf localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotprintticket"), localTicketPrinterException);
        localMessageInf.show(this);
      }
    }
  }

  private void initComponents()
  {
    this.jPanel2 = new JPanel();
    this.jPanel6 = new JPanel();
    this.btnCustomer = new JButton();
    this.btnSave = new JButton();
    this.jSeparator1 = new JSeparator();
    this.btnPay = new JButton();
    this.jPanel3 = new JPanel();
    this.jPanel4 = new JPanel();
    this.m_jKeys = new JEditorKeys();
    this.jPanel5 = new JPanel();
    this.editorcard = new JEditorString();
    this.jButton1 = new JButton();
    this.jPanel1 = new JPanel();
    this.jLabel3 = new JLabel();
    this.jLabel12 = new JLabel();
    this.jLabel5 = new JLabel();
    this.txtCard = new JTextField();
    this.jLabel1 = new JLabel();
    this.jLabel2 = new JLabel();
    this.txtCurdebt = new JTextField();
    this.txtCurdate = new JTextField();
    this.jLabel6 = new JLabel();
    this.txtName = new JTextField();
    this.txtMaxdebt = new JTextField();
    this.txtNotes = new JEditorString();
    this.txtTaxId = new JTextField();
    this.jLabel7 = new JLabel();
    setLayout(new BorderLayout());
    this.jPanel2.setLayout(new BorderLayout());
    this.btnCustomer.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/kuser.png")));
    this.btnCustomer.setFocusPainted(false);
    this.btnCustomer.setFocusable(false);
    this.btnCustomer.setMargin(new Insets(8, 14, 8, 14));
    this.btnCustomer.setRequestFocusEnabled(false);
    this.btnCustomer.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        CustomersPayment.this.btnCustomerActionPerformed(paramActionEvent);
      }
    });
    this.jPanel6.add(this.btnCustomer);
    this.btnSave.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/filesave.png")));
    this.btnSave.setFocusPainted(false);
    this.btnSave.setFocusable(false);
    this.btnSave.setMargin(new Insets(8, 14, 8, 14));
    this.btnSave.setRequestFocusEnabled(false);
    this.btnSave.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        CustomersPayment.this.btnSaveActionPerformed(paramActionEvent);
      }
    });
    this.jPanel6.add(this.btnSave);
    this.jPanel6.add(this.jSeparator1);
    this.btnPay.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/greenled.png")));
    this.btnPay.setText(AppLocal.getIntString("button.pay"));
    this.btnPay.setFocusPainted(false);
    this.btnPay.setFocusable(false);
    this.btnPay.setMargin(new Insets(8, 14, 8, 14));
    this.btnPay.setRequestFocusEnabled(false);
    this.btnPay.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        CustomersPayment.this.btnPayActionPerformed(paramActionEvent);
      }
    });
    this.jPanel6.add(this.btnPay);
    this.jPanel2.add(this.jPanel6, "Before");
    add(this.jPanel2, "First");
    this.jPanel3.setLayout(new BorderLayout());
    this.jPanel4.setLayout(new BoxLayout(this.jPanel4, 1));
    this.m_jKeys.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        CustomersPayment.this.m_jKeysActionPerformed(paramActionEvent);
      }
    });
    this.jPanel4.add(this.m_jKeys);
    this.jPanel5.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    this.jPanel5.setLayout(new GridBagLayout());
    this.jPanel5.add(this.editorcard, new GridBagConstraints());
    this.jButton1.setIcon(new ImageIcon(getClass().getResource("/com/openbravo/images/button_ok.png")));
    this.jButton1.setFocusPainted(false);
    this.jButton1.setFocusable(false);
    this.jButton1.setMargin(new Insets(8, 14, 8, 14));
    this.jButton1.setRequestFocusEnabled(false);
    this.jButton1.addActionListener(new ActionListener()
    {
      public void actionPerformed(ActionEvent paramActionEvent)
      {
        CustomersPayment.this.jButton1ActionPerformed(paramActionEvent);
      }
    });
    GridBagConstraints localGridBagConstraints = new GridBagConstraints();
    localGridBagConstraints.gridx = 1;
    localGridBagConstraints.gridy = 0;
    localGridBagConstraints.fill = 1;
    localGridBagConstraints.weightx = 1.0D;
    localGridBagConstraints.weighty = 1.0D;
    localGridBagConstraints.insets = new Insets(0, 5, 0, 0);
    this.jPanel5.add(this.jButton1, localGridBagConstraints);
    this.jPanel4.add(this.jPanel5);
    this.jPanel3.add(this.jPanel4, "North");
    add(this.jPanel3, "After");
    this.jLabel3.setText(AppLocal.getIntString("label.name"));
    this.jLabel12.setText(AppLocal.getIntString("label.notes"));
    this.jLabel5.setText(AppLocal.getIntString("label.card"));
    this.txtCard.setEditable(false);
    this.txtCard.setFocusable(false);
    this.txtCard.setRequestFocusEnabled(false);
    this.jLabel1.setText(AppLocal.getIntString("label.maxdebt"));
    this.jLabel2.setText(AppLocal.getIntString("label.curdebt"));
    this.txtCurdebt.setEditable(false);
    this.txtCurdebt.setHorizontalAlignment(4);
    this.txtCurdebt.setFocusable(false);
    this.txtCurdebt.setRequestFocusEnabled(false);
    this.txtCurdate.setEditable(false);
    this.txtCurdate.setHorizontalAlignment(0);
    this.txtCurdate.setFocusable(false);
    this.txtCurdate.setRequestFocusEnabled(false);
    this.jLabel6.setText(AppLocal.getIntString("label.curdate"));
    this.txtName.setEditable(false);
    this.txtName.setFocusable(false);
    this.txtName.setRequestFocusEnabled(false);
    this.txtMaxdebt.setEditable(false);
    this.txtMaxdebt.setHorizontalAlignment(4);
    this.txtMaxdebt.setFocusable(false);
    this.txtMaxdebt.setRequestFocusEnabled(false);
    this.txtTaxId.setEditable(false);
    this.txtTaxId.setFocusable(false);
    this.txtTaxId.setRequestFocusEnabled(false);
    this.jLabel7.setText(AppLocal.getIntString("label.taxid"));
    GroupLayout localGroupLayout = new GroupLayout(this.jPanel1);
    this.jPanel1.setLayout(localGroupLayout);
    localGroupLayout.setHorizontalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel7, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtTaxId, -2, 240, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel3, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtName, -2, 240, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel5, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtCard, -2, 240, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel12, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtNotes, -2, 270, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel1, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtMaxdebt, -2, 130, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel2, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtCurdebt, -2, 130, -2)).addGroup(localGroupLayout.createSequentialGroup().addComponent(this.jLabel6, -2, 140, -2).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addComponent(this.txtCurdate, -2, 130, -2))).addContainerGap(97, 32767)));
    localGroupLayout.setVerticalGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addGroup(localGroupLayout.createSequentialGroup().addContainerGap().addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel7).addComponent(this.txtTaxId, -2, 20, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel3).addComponent(this.txtName, -2, 20, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel5).addComponent(this.txtCard, -2, 20, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.LEADING).addComponent(this.jLabel12).addComponent(this.txtNotes, -2, 80, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel1).addComponent(this.txtMaxdebt, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel2).addComponent(this.txtCurdebt, -2, -1, -2)).addPreferredGap(LayoutStyle.ComponentPlacement.RELATED).addGroup(localGroupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE).addComponent(this.jLabel6).addComponent(this.txtCurdate, -2, -1, -2)).addContainerGap(89, 32767)));
    add(this.jPanel1, "Center");
  }

  private void jButton1ActionPerformed(ActionEvent paramActionEvent)
  {
    readCustomer();
  }

  private void m_jKeysActionPerformed(ActionEvent paramActionEvent)
  {
    readCustomer();
  }

  private void btnCustomerActionPerformed(ActionEvent paramActionEvent)
  {
    JCustomerFinder localJCustomerFinder = JCustomerFinder.getCustomerFinder(this, this.dlcustomers);
    localJCustomerFinder.search(null);
    localJCustomerFinder.setVisible(true);
    CustomerInfo localCustomerInfo = localJCustomerFinder.getSelectedCustomer();
    if (localCustomerInfo != null)
      try
      {
        CustomerInfoExt localCustomerInfoExt = this.dlsales.loadCustomerExt(localCustomerInfo.getId());
        if (localCustomerInfoExt == null)
        {
          localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotfindcustomer"));
          localMessageInf.show(this);
        }
        else
        {
          editCustomer(localCustomerInfoExt);
        }
      }
      catch (BasicException localBasicException)
      {
        MessageInf localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotfindcustomer"), localBasicException);
        localMessageInf.show(this);
      }
    this.editorcard.reset();
    this.editorcard.activate();
  }

  private void btnPayActionPerformed(ActionEvent paramActionEvent)
  {
    this.paymentdialog.setPrintSelected(true);
    if (this.paymentdialog.showDialog(this.customerext.getCurdebt().doubleValue(), null))
    {
      TicketInfo localTicketInfo = new TicketInfo();
      localTicketInfo.setTicketType(2);
      List localList = this.paymentdialog.getSelectedPayments();
      double d = 0.0D;
      Iterator localIterator = localList.iterator();
      Object localObject;
      while (localIterator.hasNext())
      {
        localObject = (PaymentInfo)localIterator.next();
        d += ((PaymentInfo)localObject).getTotal();
      }
      localList.add(new PaymentInfoTicket(-d, "debtpaid"));
      localTicketInfo.setPayments(localList);
      localTicketInfo.setUser(this.app.getAppUserView().getUser().getUserInfo());
      localTicketInfo.setActiveCash(this.app.getActiveCashIndex());
      localTicketInfo.setDate(new Date());
      localTicketInfo.setCustomer(this.customerext);
      try
      {
        this.dlsales.saveTicket(localTicketInfo, this.app.getInventoryLocation());
      }
      catch (BasicException localBasicException1)
      {
        localObject = new MessageInf(-67108864, AppLocal.getIntString("message.nosaveticket"), localBasicException1);
        ((MessageInf)localObject).show(this);
      }
      CustomerInfoExt localCustomerInfoExt;
      try
      {
        localCustomerInfoExt = this.dlsales.loadCustomerExt(this.customerext.getId());
        if (localCustomerInfoExt == null)
        {
          localObject = new MessageInf(-33554432, AppLocal.getIntString("message.cannotfindcustomer"));
          ((MessageInf)localObject).show(this);
        }
        else
        {
          editCustomer(localCustomerInfoExt);
        }
      }
      catch (BasicException localBasicException2)
      {
        localCustomerInfoExt = null;
        MessageInf localMessageInf = new MessageInf(-33554432, AppLocal.getIntString("message.cannotfindcustomer"), localBasicException2);
        localMessageInf.show(this);
      }
      printTicket(this.paymentdialog.isPrintSelected() ? "Printer.CustomerPaid" : "Printer.CustomerPaid2", localTicketInfo, localCustomerInfoExt);
    }
    this.editorcard.reset();
    this.editorcard.activate();
  }

  private void btnSaveActionPerformed(ActionEvent paramActionEvent)
  {
    if (this.dirty.isDirty())
    {
      save();
      this.editorcard.reset();
      this.editorcard.activate();
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.pos.customers.CustomersPayment
 * JD-Core Version:    0.6.0
 */